//
//  RootViewController.m
//  InTheSnow
//
//  Created by di on 7/23/09.
//  Copyright INTELLECTSOFT 2009. All rights reserved.
//
#import "UIBannerImageView.h"
#import "BannersParser.h"
#import "TopBarView.h"
#import "Loader.h"
#import "DescrViewController.h"
#import "News.h"
#import "RSSParser.h"
#import " HeaderNewsCell.h"
#import "NewssViewController.h"


@implementation NewssViewController

@synthesize contentOfTab;
@synthesize bannerHeadlines;
@synthesize toFirst = isFirst;
@synthesize thumbImagesCell;

#pragma mark ---
#pragma mark UIViewController metods
 
- (void)viewDidLoad {NSLog(@"viewDidLoad");
    [super viewDidLoad];
	
	if(__delegate.isOffline) bannerHeadlines.frame = CGRectZero;	
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload) name:@"reloadData" object:nil];	
	[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
	
	NSMutableArray *thumbImagesCell_ = [[NSMutableArray alloc] init];
	self.thumbImagesCell = thumbImagesCell_;
	[thumbImagesCell_ release]; 
	
	[__delegate.window addSubview:__delegate.toolBar ];
	
	if(!__delegate.isOffline && [[__delegate banners] count] > 0)
	{
		UIBannerImageView *bann = (UIBannerImageView*)[[__delegate banners] objectAtIndex:0];
		[self.bannerHeadlines setUrl:bann.url];
		[self.bannerHeadlines setAnimationImages:bann.animationImages];
		[self.bannerHeadlines setAnimationDuration:bann.timeout * [[bann animationImages] count]]; 		 
		[bannerHeadlines startAnimating];
		
	}
	[self reload];
}

- (void)viewWillAppear:(BOOL)animated {	NSLog(@"viewWillAppear");
	[super viewWillAppear:animated]; 
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload) name:@"reloadData" object:nil];	
	[self.tableView reloadData];
	
}

- (void)viewDidAppear:(BOOL)animated {	NSLog(@"viewDidAppear");
	[super viewDidAppear:animated];
	
	[__delegate.toolBar setHidden:NO]; 
	[self.tableView reloadData];
	
	
}

- (void)viewDidUnload {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark ---
#pragma mark Custom iDi

- (void) loadImgs {
	
	NSAutoreleasePool	 *autoreleasepooll = [[NSAutoreleasePool alloc] init];
	
	[thumbImagesCell removeAllObjects]; 
	
	for (int i = 0; i < [contentOfTab count]; i++) 
	{
		id n =[contentOfTab objectAtIndex:i]; 
		NSData *thumbImageData = [[[NSData alloc] initWithData:[NSData dataWithContentsOfURL:[(News*)n thumbImage]]] autorelease];
		
		if(thumbImageData == nil || [__delegate isOffline])	{	[thumbImagesCell addObject:[NSNull null]]; 	NSLog(@"thumb img is nil");	}
		else{	[thumbImagesCell addObject:thumbImageData];   	NSLog(@"imaga ist");}		 
		
	}	 	
	 	
	[self performSelectorOnMainThread:@selector(reload) withObject:nil waitUntilDone:NO];
	[autoreleasepooll release];
	
}

-(void) reload{  
	[self.tableView reloadData];
	if(!isFirst){
		
		isFirst = YES; 
		
		NSInvocationOperation *loadOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImgs) object:nil];
		queue = [[NSOperationQueue alloc] init] ;
		[queue addOperation:loadOperation];
		[loadOperation release];
		[queue release]; 
	} 
} 

#pragma mark ---
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSLog(@"%i %i",[contentOfTab count], section);
	return [contentOfTab count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
	
	HeaderNewsCell *cell =(HeaderNewsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"HeaderNewsCell" owner:self options:nil];
		cell = [nibs objectAtIndex:0];		  
	}
	
	if ([contentOfTab count] > indexPath.row) {	 
		News *news = [contentOfTab objectAtIndex:indexPath.row]; 
		[cell.bodyLbl setFont:[UIFont fontWithName:@"Helvetica" size:11]];
		[[cell titleLbl] setText:news.title];
		[[cell bodyLbl] setText:news.description];   
		[cell setThubmImageUrl:news.thumbImage]; 
		if([thumbImagesCell count] > indexPath.row){
			if([thumbImagesCell objectAtIndex:indexPath.row] == [NSNull null]) 
			{
				[cell.thumbImage setImage:[UIImage imageNamed:@"icon.png"]];
				return cell;
			}
			if([thumbImagesCell objectAtIndex:indexPath.row] != nil)[cell.thumbImage setImage:[UIImage imageWithData:[thumbImagesCell objectAtIndex:indexPath.row]]]; 
			else 	[cell.thumbImage setImage:[UIImage imageNamed:@"icon.png"]];
		}
		else 	[cell.thumbImage setImage:[UIImage imageNamed:@"icon.png"]];
	}
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	News *news = [contentOfTab objectAtIndex:indexPath.row];	
	
	DescrViewController *tmp = [[DescrViewController alloc] initWithNibName:@"DescrViewController" bundle:nil news:news url:news.bigImage];
	[self.navigationController pushViewController:tmp animated:YES];
	[tmp release];
}

#pragma mark ---
#pragma mark Memory Managment
 
- (void)dealloc {
	//	NSLog(@"[newscontrl dealloc]"); 
	
	[bannerHeadlines release];
	[queue release];
	[thumbImagesCell release];
	[super dealloc];
}


@end

